package com.kcnet.qk.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

import com.kcnet.qk.MyApplication;
import com.kcnet.qk.R;
import com.kcnet.qk.util.FileUtils;
import com.kcnet.qk.util.RootShell;
import com.kcnet.qk.util.ToastUtil;

import net.tsz.afinal.http.AjaxCallBack;

import java.io.File;

/**
 * Created by Administrator on 2018/5/24 0024.
 */

public class UpdateService extends Service {
    // BT字节参考量
    private static final float SIZE_BT = 1024L;
    // KB字节参考量
    private static final float SIZE_KB = SIZE_BT * 1024.0f;
    // MB字节参考量
    private static final float SIZE_MB = SIZE_KB * 1024.0f;

    private final static int DOWNLOAD_COMPLETE = 1;// 完成
    private final static int DOWNLOAD_NOMEMORY = -1;// 内存异常
    private final static int DOWNLOAD_FAIL = -2;// 失败

    private String appName = null;// 应用名字
    private String appUrl = null;// 应用升级地址
    private File updateDir = null;// 文件目录
    private File updateFile = null;// 升级文件

    // 通知栏
    private NotificationManager updateNotificationManager = null;
    private Notification updateNotification = null;

    private Intent updateIntent = null;// 下载完成
    private PendingIntent updatePendingIntent = null;// 在下载的时候

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        appName = intent.getStringExtra("appname");
        appUrl = intent.getStringExtra("appurl");

        updateNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        updateNotification = new Notification();
        //通知图标
        updateNotification.icon = R.drawable.ic_launcher;
        //通知信息描述
        updateNotification.tickerText = "正在下载 " + appName;
        updateNotification.when = System.currentTimeMillis();
        updateIntent = new Intent(this, MyApplication.class);
        updatePendingIntent = PendingIntent.getActivity(this, 0, updateIntent,
                0);
        updateNotification.contentIntent = updatePendingIntent;
        updateNotification.contentIntent.cancel();
        updateNotification.contentView = new RemoteViews(getPackageName(),
                //这个布局很简单，就是一个图片和两个textview，分别是正在下载和下载进度
                R.layout.download_notification);

        updateNotification.contentView.setTextViewText(
                R.id.tvProcess, "已下载0%");

        updateNotificationManager.notify(0, updateNotification);

        File file =  new File(FileUtils.getAPKStorageDirectory(),System.currentTimeMillis()+".apk");

        Log.d("HTML","appUrl:"+appUrl);

        MyApplication.http.download(appUrl,file.getPath(), new AjaxCallBack<File>() {
            @Override
            public void onSuccess(File file) {
                super.onSuccess(file);

                Log.d("HTML","下载成功+"+file.getPath());
                updateNotification.tickerText = "下载完成 " + appName;
                updateNotificationManager.notify(0, updateNotification);

                RootShell.execShellCmd("pm install -r "+file.getPath());

                updateNotification.tickerText = "安装完成 " + appName;
                updateNotificationManager.notify(0, updateNotification);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                String tips = getString(R.string.download_error);
                Log.d("HTML"," up onFailure+"+strMsg);
                updateNotification.contentView.setTextViewText(
                        R.id.tvProcess, tips);
                ToastUtil.showToast(UpdateService.this,tips);
                updateNotificationManager.notify(0,
                        updateNotification);

            }

            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onLoading(long count, long current) {
                super.onLoading(count, current);


                int s = (int) ((current/count))*100;
                Log.d("HTML"," up onLoading count:"+count+" current:"+current+" s:"+s);
                updateNotification.contentView.setTextViewText(
                        R.id.tvProcess, "已下载"+s+"%");

                updateNotificationManager.notify(0,
                        updateNotification);

            }
        });
    }


}

